package com.yan.test.work.testpaging3

import android.os.Bundle
import android.os.PersistableBundle
import android.view.KeyEvent
import android.view.LayoutInflater
import android.view.inputmethod.EditorInfo
import android.widget.SearchView
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.lifecycleScope
import androidx.paging.PagingData
import androidx.recyclerview.widget.LinearLayoutManager
import com.yan.test.work.testpaging3.databinding.ActivityMainBinding
import com.yan.test.work.testpaging3.net.L
import kotlinx.coroutines.launch

/**
 * Create By : 'YWY'
 * On : 2024/4/5
 */
class MainActivity2 : AppCompatActivity(){

    lateinit var mBinding:ActivityMainBinding

    lateinit var adapter:KTLAdapter

    val mModel by lazy {
        ViewModelProvider(this).get(OSCViewModel::class.java)
    }


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        L.d("onCreate MainPage")
        mBinding = ActivityMainBinding.inflate(LayoutInflater.from(this))
        setContentView(mBinding.root)
        Toast.makeText(this, "run", Toast.LENGTH_SHORT).show()


        initView()
        initListener()
        initData()


    }

    private fun submitData(pagingData: PagingData<DataBean>) {
        lifecycleScope.launch {
            adapter.submitData(pagingData)
        }
    }


    private fun initView() {
        mBinding.listView.layoutManager = LinearLayoutManager(this , LinearLayoutManager.VERTICAL , false)
        adapter = KTLAdapter()
        val contectAdapter = adapter.withLoadStateFooter(LoadingStatusAdapter())
        mBinding.listView.adapter = contectAdapter
        mBinding.refreshLayout.setOnRefreshListener {
            adapter.refresh()
        }

        lifecycleScope.launch {
            adapter.onPagesUpdatedFlow.collect{
                val hasData = adapter.itemCount > 0
                mBinding.refreshLayout.isRefreshing = false
            }
        }

        mBinding.searchView.setOnQueryTextListener(object : SearchView.OnQueryTextListener{
            override fun onQueryTextSubmit(query: String?): Boolean {
                (mBinding.listView.layoutManager as LinearLayoutManager).scrollToPosition(0)
                lifecycleScope.launch {
                    mModel.getDataFlow(this@MainActivity2 , query!!.trim()).collect{
                        submitData(it)
                    }
                }
                return true
            }

            override fun onQueryTextChange(newText: String?): Boolean = true

        })


        mBinding.searchInput.setOnEditorActionListener { v, actionId, event ->

            if (actionId == EditorInfo.IME_ACTION_SEARCH ||
                (event != null && event.keyCode == KeyEvent.KEYCODE_ENTER)) {
                (mBinding.listView.layoutManager as LinearLayoutManager).scrollToPosition(0)
                lifecycleScope.launch {
                    mModel.getDataFlow( this@MainActivity2 ,mBinding.searchInput.text.toString().trim()).collect{
                        submitData(it)
                    }
                }
                true // 消耗此事件，不再传递给其他监听器
            } else {
                false // 默认处理或继续传递事件
            }
        }
    }

    private fun initListener() {

    }

    private fun initData() {

    }
}